<template>
  <div class="user-div">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="5">
          <el-button type="primary" @click="handleAdd">添加用户</el-button>
        </el-col>
      </el-row>

      <el-table border style="width: 100%; margin-top:20px" :data="userList">
        <el-table-column label="注册日期" prop="createTime"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="年龄" prop="age"></el-table-column>
        <el-table-column label="手机号" prop="phone"></el-table-column>
        <el-table-column label="余额" prop="money"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="handleDelet(scope.$index,scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :current-page="currentPage"
        @current-change="handleCurrentChange"
      ></el-pagination>

      <!-- 修改用户表单弹框 -->
      <el-dialog :visible.sync="editUserFormVisible" width="30%">
        <el-form :model="editUserForm" label-width="80px">
          <el-form-item label="ID">
            <el-input v-model="editUserForm.id" disabled></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="editUserForm.username"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="editUserForm.age"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="editUserForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="余额">
            <el-input v-model="editUserForm.money"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editUserFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUser">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 添加用户表单弹框 -->
      <el-dialog :visible.sync="addUserFormVisible" width="30%">
        <el-form :model="addUserForm" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="addUserForm.username"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="addUserForm.age"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="addUserForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="余额">
            <el-input v-model="addUserForm.money"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addUserFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLogin: false,
      editUserFormVisible: false,
      addUserFormVisible: false,
      currentPage: 1,
      userList: [],
      editUserForm: {
        id: "",
        username: "",
        gender: "",
        age: "",
        phone: "",
        money: "",
        createTime: ""
      },
      addUserForm: {
        username: "",
        gender: "",
        age: "",
        phone: "",
        money: "",
        createTime: ""
      }
    };
  },
  mounted() {
    this.currentPage = 1;
    this.getUserList(this.currentPage);
  },
  methods: {
    // 点击编辑按钮
    handleEdit(row) {
      // console.log(row);
      this.editUserForm = row;
      this.editUserFormVisible = true;
    },

    // 点击删除按钮
    handleDelet(index, row) {
      this.deletUser(index, row);
    },

    // 点击添加按钮
    handleAdd() {
      this.addUserFormVisible = true;
    },

    // 增加用户信息
    async addUser() {
      this.addUserFormVisible = false;
      // console.log(this.addUserForm);

      // const { data: res } = await this.$http.post(
      //   "/admin/updateUser",
      //   this.addUserForm
      // );
      // if (res.code == 200) {
      //   // 删除成功提示
      //   this.$message({
      //     type: "success",
      //     message: "修改成功!"
      //   });
      // } else {
      //   this.$message({
      //     type: "info",
      //     message: "修改失败"
      //   });
      // }
    },

    // 修改用户信息
    async editUser() {
      this.editUserFormVisible = false;
      console.log(this.editUserForm);

      const { data: res } = await this.$http.post(
        "/admin/updateUser",
        this.editUserForm
      );
      if (res.code == 200) {
        // 删除成功提示
        this.$message({
          type: "success",
          message: "修改成功!"
        });
      } else {
        this.$message({
          type: "info",
          message: "修改失败"
        });
      }
    },

    // 删除用户
    deletUser(index, row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {

          const { data: res } = await this.$http.post("/admin/deleteUser", row);

          if (res.code == 200) {
            this.userList.splice(index, 1);
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //获得所有用户列表

    async getUserList(page) {
      const { data: res } = await this.$http.get("/admin/getUserList/" + page);
      if (res.code == 200) {
        this.userList = res.data.userList;
      } else {
        this.$message({
          type: "info",
          message: "获取用户列表失败"
        });
      }
    },

    handleCurrentChange(val) {
      // console.log(val);
      this.currentPage = val;
      this.getUserList(val);
    }
  }
};
</script>

<style lang="less" scoped>
.user-div {
  width: 80%;
  margin: auto;
}
</style>>
